package router

import (
	"comic-mgmt-sys/handle"
	"net/http"

	"github.com/gin-gonic/gin"
)

func Router() *gin.Engine {
	r := gin.Default()
	r.Use(Cors()) //默认跨域
	// 2.绑定路由规则，执行的函数
	// gin.Context，封装了request和response
	r.POST("/login", handle.Login)
	r.POST("/create", handle.Create)
	r.GET("/list", handle.List)
	r.GET("/detail", handle.Detail)
	r.GET("/del", handle.Del)
	r.GET("/userOrderList", handle.UserOrderList)     //角色列表
	r.GET("/userOrderDetail", handle.UserOrderDetail) //角色详情
	r.GET("/carOrderList", handle.CarOrderList)       //汽车列表
	r.GET("/userOrder", handle.UserOrder)             //角色列表
	r.GET("/orderNode", handle.OrderNode)             //权限列表
	r.GET("/carOrderDetail", handle.CarOrderDetail)   //汽车详情
	return r
}
func Cors() gin.HandlerFunc {
	return func(c *gin.Context) {
		method := c.Request.Method
		origin := c.Request.Header.Get("Origin")
		if origin != "" {
			c.Header("Access-Control-Allow-Origin", "*")
			c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
			c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization")
			c.Header("Access-Control-Allow-Credentials", "true")
			c.Set("content-type", "application/json")
		}
		//放行所有OPTIONS方法
		if method == "OPTIONS" {
			c.AbortWithStatus(http.StatusNoContent)
		}
		c.Next()
	}
}
